﻿using PSAMMatrixCloud.Model;
using System;
using System.Linq;
using System.Text;

namespace PSAMMatrixCloud.TransferSecret.LaneMutual
{
    public class Message
    {
        public static byte[] ProtocolHead = { 0x55,0xAA};
        public static short ProtocolPackageMinLength = 6;

        public Message(int commandType, byte messageID)
        {
            CommandType = commandType;
            MessageID = messageID;
            SendTime = DateTime.Now;
            switch ((ServerCommandTypeReply)commandType)
            {
                case ServerCommandTypeReply.HeartBeat:
                    Body = new byte[9];
                    break;

                case ServerCommandTypeReply.IdentityAuth:
                    Body = new byte[5];
                    break;

                case ServerCommandTypeReply.VehicleInfo:
                    Body = new byte[28];
                    break;

                case ServerCommandTypeReply.MAC1:
                    Body = new byte[36];
                    break;

                case ServerCommandTypeReply.MAC2:
                    Body = new byte[16];
                    break;
                default:
                    Body = new byte[0];
                    break;
            }
        }

        public Message(byte[] data)
        {
            byte[] headBytes = new byte[2];
            byte[] lengthBytes = new byte[2];
            byte[] messageIDBytes = new byte[1];
            byte[] typeBytes = new byte[1];
            Body = new byte[data.Length - 7];

            Array.ConstrainedCopy(data, 0, headBytes, 0, 2);
            Array.ConstrainedCopy(data, 2, messageIDBytes, 0, 1);
            Array.ConstrainedCopy(data, 3, lengthBytes, 0, 2);
            Array.ConstrainedCopy(data, 5, typeBytes, 0, 1);
            Array.ConstrainedCopy(data, 6, Body, 0, data.Length - 7);
            CheckValue = data[data.Length - 1];
            Length = data.Length;
            MessageID = messageIDBytes[0];
            CommandType = (int)typeBytes[0];
            ReceviceTime = DateTime.Now;
        }

        /// <summary>
        /// 数据主体
        /// </summary>
        public byte[] Body { get; set; }

        /// <summary>
        /// 指令类型
        /// </summary>
        public int CommandType { get; set; }

        public long Length { get; set; }

        /// <summary>
        /// 消息ID
        /// </summary>
        public byte MessageID { get; set; }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime SendTime { get; set; }

        /// <summary>
        /// 校验值
        /// </summary>
        public byte CheckValue { get; set; }

        /// <summary>
        /// 收到时间
        /// </summary>
        public DateTime ReceviceTime { get; set; }

        public void Clear()
        {
            Body = new byte[0];
        }

        public byte[] ToBytes()
        {
            byte[] buffer = new byte[Body.Length + 7];

 
            byte[] lengthBytes = BitConverter.GetBytes((short)Body.Length);
            byte messageIDBytes = MessageID;


            if (!BitConverter.IsLittleEndian) // 若为 大端模式
            {
                Array.Reverse(lengthBytes); // 转换为   小端模式             
            }
            Array.ConstrainedCopy(ProtocolHead, 0, buffer,0, 2);
            buffer[2] = MessageID;
            Array.ConstrainedCopy(lengthBytes, 0, buffer, 3, 2);

            buffer[5] = (byte)CommandType;

            if (Body != null && Body.Length != 0)
            {
                Array.ConstrainedCopy(Body.ToArray(), 0, buffer, 6, Body.Length);
            }
            //计算校验
            buffer[buffer.Length-1]=(byte)PSAMMatrixCloud.Common.Check.CheckXor(buffer, 2, buffer.Length-3);
            return buffer;
        }
    }
}